import { DataTypes, Model } from 'sequelize';
import sequelize from '../db';

/**
 * 菜单类型
 */
export enum MenuType {
  DIRECTORY = 'directory', // 目录
  MENU = 'menu',           // 菜单
  BUTTON = 'button',       // 按钮
}

/**
 * 菜单模型
 */
class Menu extends Model {
  public id!: string;
  public tenantId!: string | null; // null 表示系统菜单
  public parentId!: string | null;
  public name!: string;
  public title!: string;
  public english?: string;
  public type!: MenuType;
  public path?: string;
  public component?: string;
  public icon?: string;
  public permission?: string;
  public orderNum!: number;
  public visible!: boolean;
  public status!: boolean;
  public cache!: boolean;
  public redirect?: string;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 子菜单（用于前端树形结构）
  public children?: Menu[];
}

Menu.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    tenantId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'tenant_id',
      comment: '租户ID，为null表示系统菜单',
    },
    parentId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'parent_id',
      comment: '父菜单ID',
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      comment: '菜单名称（唯一标识）',
    },
    title: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '菜单标题（显示名称）',
    },
    english: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '英文名称',
    },
    type: {
      type: DataTypes.ENUM('directory', 'menu', 'button'),
      allowNull: false,
      defaultValue: 'menu',
      comment: '菜单类型',
    },
    path: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '路由路径',
    },
    component: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '组件路径',
    },
    icon: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '图标',
    },
    permission: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '权限标识',
    },
    orderNum: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      field: 'order_num',
      comment: '排序序号',
    },
    visible: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: '是否显示',
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: '状态（启用/禁用）',
    },
    cache: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否缓存',
    },
    redirect: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '重定向路径',
    },
  },
  {
    sequelize,
    tableName: 'menus',
    underscored: true,
    timestamps: true,
    indexes: [
      {
        fields: ['tenant_id'],
      },
      {
        fields: ['parent_id'],
      },
      {
        fields: ['type'],
      },
      {
        fields: ['order_num'],
      },
    ],
  },
);

export default Menu;

